package com.example.test06thread.callable;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @Author: fxy
 * @Date: 2022/05/27/9:48
 * @Description:
 * 使用callable会有缓存
 * get方法可能会发生阻塞
 */
public class CallableTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        MyThread thread = new MyThread();
        //适配类
        FutureTask<String> futureTask = new FutureTask<>(thread);
        new Thread(futureTask,"A").start();
        //有缓存，所以控制台只打印了一次call方法
        new Thread(futureTask,"B").start();
        //get可能会存在阻塞，解决放到最后，或者使用异步通信处理
        String s = futureTask.get();
        System.out.println(s);

    }
}
class MyThread implements Callable<String> {

    @Override
    public String call() throws Exception {
        System.out.println("执行了call()方法");
        return "1024";
    }
}
